/*
 * Copyright (C) 2010 Leandro Aparecido <lehphyro@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.mjl.impl;

import com.google.code.mjl.*;

public class LogImpl implements Log {

	private final String categoryName;
	private final boolean async;
	private final LogAdapter logAdapter;

	public LogImpl(String categoryName, boolean async, LogAdapter logAdapter) {
		this.categoryName = categoryName;
		this.async = async;
		this.logAdapter = logAdapter;
	}

	public boolean enabled(Log.Level level) {
		return logAdapter.isEnabled(level);
	}

	public void trace(String formatMessage, Object... formatParams) {
		if (enabled(Log.Level.TRACE)) {
			log(LogEvent.create(logAdapter, categoryName, Log.Level.TRACE, formatMessage, formatParams), async);
		}
	}

	public void trace(Throwable t, String formatMessage, Object... formatParams) {
		if (enabled(Log.Level.TRACE)) {
			log(LogEvent.create(logAdapter, categoryName, Log.Level.TRACE, formatMessage, formatParams, t), async);
		}
	}

	public void debug(String formatMessage, Object... formatParams) {
		if (enabled(Log.Level.DEBUG)) {
			log(LogEvent.create(logAdapter, categoryName, Log.Level.DEBUG, formatMessage, formatParams), async);
		}
	}

	public void debug(Throwable t, String formatMessage, Object... formatParams) {
		if (enabled(Log.Level.DEBUG)) {
			log(LogEvent.create(logAdapter, categoryName, Log.Level.DEBUG, formatMessage, formatParams, t), async);
		}
	}

	public void info(String formatMessage, Object... formatParams) {
		if (enabled(Log.Level.INFO)) {
			log(LogEvent.create(logAdapter, categoryName, Log.Level.INFO, formatMessage, formatParams), async);
		}
	}

	public void info(Throwable t, String formatMessage, Object... formatParams) {
		if (enabled(Log.Level.INFO)) {
			log(LogEvent.create(logAdapter, categoryName, Log.Level.INFO, formatMessage, formatParams, t), async);
		}
	}

	public void warn(String formatMessage, Object... formatParams) {
		if (enabled(Log.Level.WARN)) {
			log(LogEvent.create(logAdapter, categoryName, Log.Level.WARN, formatMessage, formatParams), async);
		}
	}

	public void warn(Throwable t, String formatMessage, Object... formatParams) {
		if (enabled(Log.Level.WARN)) {
			log(LogEvent.create(logAdapter, categoryName, Log.Level.WARN, formatMessage, formatParams, t), async);
		}
	}

	public void error(String formatMessage, Object... formatParams) {
		if (enabled(Log.Level.ERROR)) {
			log(LogEvent.create(logAdapter, categoryName, Log.Level.ERROR, formatMessage, formatParams), async);
		}
	}

	public void error(Throwable t, String formatMessage, Object... formatParams) {
		if (enabled(Log.Level.ERROR)) {
			log(LogEvent.create(logAdapter, categoryName, Log.Level.ERROR, formatMessage, formatParams, t), async);
		}
	}

	public void fatal(String formatMessage, Object... formatParams) {
		if (enabled(Log.Level.FATAL)) {
			log(LogEvent.create(logAdapter, categoryName, Log.Level.FATAL, formatMessage, formatParams), async);
		}
	}

	public void fatal(Throwable t, String formatMessage, Object... formatParams) {
		if (enabled(Log.Level.FATAL)) {
			log(LogEvent.create(logAdapter, categoryName, Log.Level.FATAL, formatMessage, formatParams, t), async);
		}
	}

	@Override
	public void log(Level level, boolean asynchronous, String formatMessage, Object... formatParams) {
		if (enabled(level)) {
			log(LogEvent.create(logAdapter, categoryName, level, formatMessage, formatParams), asynchronous);
		}
	}

	@Override
	public void log(Level level, Throwable t, boolean asynchronous, String formatMessage, Object... formatParams) {
		if (enabled(level)) {
			log(LogEvent.create(logAdapter, categoryName, level, formatMessage, formatParams, t), asynchronous);
		}
	}

	protected void log(LogEvent logEvent, boolean asynchronous) {
		LogManager.log(logEvent, asynchronous);
	}
}
